package com.cc.visitor.utils;

import com.cc.alltype.ResponseResult;
import com.cc.visitor.dao.ConfigMapper;
import com.cc.visitor.entity.Config;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;

/**
 * @author chanchaw
 * @create 2025-05-16 8:34
 */
@Service
public class HomeServeUtils {
    @Autowired
    private ConfigMapper configMapper;
    //private static final String TIME_CONTROL_URL = "http://10.8.2.122:7005/homeservebe/dev/execLinux";
    private static final String CMD = "/etc/init.d/timecontrol";
    @Autowired
    private RestTemplateUtils restTemplateUtils;

    public ResponseResult openInternet(Boolean open) throws JsonProcessingException {
        String url = "http://10.8.2.122:7005/homeservebe/dev/execLinux";
        Config timeControlUrl = configMapper.get8sid("timeControlUrl");
        if(timeControlUrl != null){
            String datas = Optional.ofNullable(timeControlUrl.getDatas()).orElse("");
            if(datas.length() > 0) url = datas;
        }

        String cmd = CMD;
        if(open == true) cmd = cmd + " stop";
        else cmd = cmd + " start";
        return restTemplateUtils.postJson(url, cmd, ResponseResult.class);
    }
}
